//
//  CLHomeViewController.m
//  CareLink
//
//  Created by apple on 16/8/18.
//  Copyright © 2016年 Seven Team. All rights reserved.
//

#import "CLHomeViewController.h"
#import "CLHomeWeather.h"
#import "CLLeftDrawerTableViewController.h"
#import "CLNetworkManager.h"
#import "CLSickCollectionViewCell.h"
#import "DoctorSelectTableViewController.h"
#import "MMDrawerBarButtonItem.h"
#import "SDCycleScrollView.h"
#import "UIViewController+MMDrawerController.h"
#import "CLDiseaseDetailsController.h"
#import "PUBLIC.h"


static NSString* cellid = @"sickCellId";
@interface CLHomeViewController () <UICollectionViewDataSource, UICollectionViewDelegate, SDCycleScrollViewDelegate>
/**
 *  轮播器视图
 */
@property (nonatomic, strong) SDCycleScrollView* scrollView;
/**
 *  底部病症视图
 */
@property (nonatomic, strong) UICollectionView* sickCollectionView;
/**
 *  UICollectionViewFlowLayout
 */
@property (nonatomic, strong) UICollectionViewFlowLayout* layout;
/**
 *  疾病数据数组
 */
@property (nonatomic, strong) NSArray* sickData;
@end

@implementation CLHomeViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // 设置UI
    [self seupUI];
    // 从plist加载数据
    [self loadSickData];
    // 设置self.navigationController.navigationBar.backgroundColor
    [self setNav];
    self.navigationItem.title = @"快医";

    // 设置左侧item
    [self setupLeftMenuButton];
    self.view.bounds = [UIScreen mainScreen].bounds;
    self.view.backgroundColor = [UIColor colorWithHex:0xf5f5f5];

    // 设置返回的按钮的文字为空
    self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil];


}
// 设置顶部视图
- (void)setNav
{
    // 设置背景图片
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"nav"] forBarMetrics:UIBarMetricsDefault];
    // 去除线
    [self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];
    // 修改颜色,记得导入UIColor+Addition
    [self.navigationController.navigationBar setBarTintColor:[UIColor colorWithHex:0x3a3a3a]];
    // translucent半透明的,设置为半透明
    self.navigationController.navigationBar.translucent = NO;
}

// 获取疾病collectionviewcell的路径
- (void)loadSickData
{
    // 获取路径
    NSString* plistPath = [[NSBundle mainBundle] pathForResource:@"SickCollectionView.plist" ofType:nil];
    NSArray* json = [[NSArray alloc] initWithContentsOfFile:plistPath];
    // 字典转模型
    NSArray* sickData = [NSArray yy_modelArrayWithClass:[NSClassFromString(@"CLSickModel") class] json:json];
    // 赋值给全局变量
    self.sickData = sickData;
}
// 设置主界面的UI
- (void)seupUI
{
    // 天气view
    //    UIView *weatherView = [UIView new];
    //    weatherView.backgroundColor = [UIColor yellowColor];

    CLHomeWeather* vc = [[CLHomeWeather alloc] init];
    UIView* weatherView = vc.view;

    [self addChildViewController:vc];

    [self.view addSubview:weatherView];
    // 天气视图约束
    [weatherView mas_makeConstraints:^(MASConstraintMaker* make) {
        make.left.right.offset(0);
        make.top.offset(1);
        make.height.offset(70);
    }];

    // 轮播器图片数组
    NSMutableArray* arr = [NSMutableArray array];
    for (int i = 1; i < 5; i++) {
        NSString* str = [NSString stringWithFormat:@"h%d", i];
        UIImage* image = [UIImage imageNamed:str];
        [arr addObject:image];
    }
    // 本地加载轮播器
    SDCycleScrollView* scrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectZero imageNamesGroup:arr];
    self.automaticallyAdjustsScrollViewInsets = NO;
    // 设置代理监听点击事件
    scrollView.delegate = self;
    // 将轮播器视图赋值给全局变量
    self.scrollView = scrollView;
    // 设置轮播器风格
    [self setScrollViewStyle];

    [self.view addSubview:scrollView];
    // 添加轮播器约束
    [scrollView mas_makeConstraints:^(MASConstraintMaker* make) {
        make.top.equalTo(weatherView.mas_bottom).offset(1);
        make.left.right.offset(0);
        make.height.offset(230);
    }];

    // 名医通视图
    UIView* doctorView = [UIView new];
    doctorView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:doctorView];
    UIImageView* imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"yj"]];
    UILabel* nameLabel = [UILabel labelWithText:@"名医通" andTextColor:[UIColor colorWithHex:0x000000] andFontSize:15];
    // 添加到父视图
    [doctorView addSubview:imageView];
    [doctorView addSubview:nameLabel];
    // 设置约束
    [imageView mas_makeConstraints:^(MASConstraintMaker* make) {
        make.top.offset(15);
        make.left.offset(12);
    }];
    [nameLabel mas_makeConstraints:^(MASConstraintMaker* make) {
        make.left.equalTo(imageView.mas_right).offset(10);
        make.top.offset(15);
    }];
    //doctorView约束
    [doctorView mas_makeConstraints:^(MASConstraintMaker* make) {
        make.top.equalTo(scrollView.mas_bottom).offset(1);
        make.right.left.offset(0);
        make.height.offset(44.5);
    }];

    // flowlayout
    UICollectionViewFlowLayout* layout = [[UICollectionViewFlowLayout alloc] init];
    // 底部病症collectionView视图
    UICollectionView* sickCollectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];

    // 添加到父视图
    [self.view addSubview:sickCollectionView];
    // 约束
    [sickCollectionView mas_makeConstraints:^(MASConstraintMaker* make) {
        make.left.right.offset(0);
        make.bottom.offset(0);
        make.top.equalTo(doctorView.mas_bottom).offset(1);
    }];
    // 设置数据源
    sickCollectionView.dataSource = self;
    sickCollectionView.delegate = self;
    [sickCollectionView registerNib:[UINib nibWithNibName:@"CLSickCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:cellid];
    // 底部病症collectionView视图全局化
    self.sickCollectionView = sickCollectionView;
    self.layout = layout;
}
#pragma mark 设置轮播器风格
- (void)setScrollViewStyle
{
    // 轮播视图风格设置
    self.scrollView.pageControlStyle = SDCycleScrollViewPageContolStyleAnimated;
    self.scrollView.pageControlAliment = SDCycleScrollViewPageContolAlimentRight;
    self.scrollView.pageControlDotSize = CGSizeMake(5, 5);
    self.scrollView.bannerImageViewContentMode = UIViewContentModeScaleAspectFill;
    self.scrollView.currentPageDotColor = [UIColor whiteColor];

}
// 添加navtabbar左侧按钮
- (void)setupLeftMenuButton
{
    //创建按钮
    MMDrawerBarButtonItem* leftDrawerButton = [[MMDrawerBarButtonItem alloc] initWithTarget:self action:@selector(leftDrawerButtonPress:)];
    leftDrawerButton.image = [UIImage imageNamed:@"leftDrawerButton"];
    leftDrawerButton.tintColor = [UIColor whiteColor];
    //为navigationItem添加LeftBarButtonItem
    [self.navigationItem setLeftBarButtonItem:leftDrawerButton animated:YES];
}
//抽屉按钮动作
- (void)leftDrawerButtonPress:(id)sender
{
    //开关左抽屉
    [self.mm_drawerController toggleDrawerSide:MMDrawerSideLeft animated:YES completion:nil];
}

#pragma mark - <UICollectionViewDataSource>

- (NSInteger)collectionView:(UICollectionView*)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.sickData.count;
}
- (UICollectionViewCell*)collectionView:(UICollectionView*)collectionView cellForItemAtIndexPath:(NSIndexPath*)indexPath
{
    CLSickCollectionViewCell* cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellid forIndexPath:indexPath];
    cell.sickModel = self.sickData[indexPath.row];
    cell.backgroundColor = [UIColor whiteColor];
    return cell;
}
#pragma mark - <UICollectionViewDelegate>
- (void)collectionView:(UICollectionView*)collectionView didSelectItemAtIndexPath:(NSIndexPath*)indexPath
{
    CLSickModel* sickModel = self.sickData[indexPath.row];
    
    // 判断是否公益活动
    if (indexPath.row == 5) {
        PUBLIC *vc = [PUBLIC new];
        [self.navigationController pushViewController:vc animated:YES];
        return;
    }
    
    CLDiseaseDetailsController *diseaseDetailCon = [[CLDiseaseDetailsController alloc]init];
    diseaseDetailCon.ci1_id = indexPath.row;
    [self.navigationController pushViewController:diseaseDetailCon animated:YES];
    
    NSLog(@"%zd", indexPath.row);
}
// 布局子控件
- (void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    // 设置collectionview的大小
    // itsize
    self.layout.itemSize = CGSizeMake((self.sickCollectionView.bounds.size.width ) / 3, (self.sickCollectionView.bounds.size.height ) / 2);
    self.layout.minimumLineSpacing = 0;
    self.layout.minimumInteritemSpacing = 0;
}
#pragma mark -<SDCycleScrollViewDelegate>
- (void)cycleScrollView:(SDCycleScrollView*)cycleScrollView didSelectItemAtIndex:(NSInteger)index
{

//    NSLog(@"图片被点击了%zd", index);
//    UIViewController* vc = [[DoctorSelectTableViewController alloc] init];
//    //    vc.view.backgroundColor =[UIColor greenColor];
//    [self.navigationController pushViewController:vc animated:YES];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
